<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="doAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button v-if="scope.row.type===1" type="text" @click="doAdd(2,scope.row.id)">添加</el-button>
              <el-button type="text" @click="editPer(scope.row.id)">编辑</el-button>
              <el-button type="text" @click="delPer(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹窗 -->
      <el-dialog
        :visible.sync="showDialog"
        :title="isEdit?'编辑权限':'新增权限'"
        :is-edit="isEdit"
        @close="hClose"
      >
        <!-- 表单内容 -->
        <el-form ref="formRef" label-width="100px" :model="formData" :rules="formRules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="showDialog = false">取消</el-button>
            <el-button type="primary" @click="doSubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permissions'
import { array2Tree } from '@/utils/index'
export default {
  // 0.数据
  data() {
    return {
      // 0.1表格数据
      list: [],
      // 0.2表单数据
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      // 0.3弹窗布尔值
      showDialog: false,
      isEdit: false,
      // 表单校验
      formRules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
        description: [{ required: true, message: '请输入权限描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 1.1获取表格数据
    this.loadPermission()
  },
  // 2.方法
  methods: {
    // 2.1查询
    async loadPermission() {
      const res = await getPermissionList()
      this.list = array2Tree(res.data, '0')
    },
    // 2.2删除
    delPer(id) {
      this.$confirm('请问您确定要删除吗?', '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.loadPermission()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    },
    // 2.3点击添加权限
    doAdd(type, pid) {
      this.isEdit = false
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 2.4点击确定按钮
    async doSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.isEdit ? this.hEdit() : this.hAdd()
        }
      })
    },
    // 2.5弹窗关闭时触发
    hClose() {
      // 表单清空
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '',
        pid: '',
        type: ''
      }
      // 清空校验
      this.$refs.formRef.resetFields()
    },
    // 2.6编辑按钮
    async editPer(id) {
      this.isEdit = true
      this.showDialog = true
      const res = await getPermissionDetail(id)
      this.formData = { ...res.data }
    },
    // 2.7添加的确认按钮
    async hAdd() {
      await addPermission(this.formData)
      this.$message.success('添加成功')
      this.showDialog = false
      this.loadPermission()
    },
    // 2.8编辑的确认按钮
    async hEdit() {
      await updatePermission(this.formData)
      this.$message.success('修改成功')
      this.showDialog = false
      this.loadPermission()
    }
  }
}
</script>
